import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import {TABLE} from '../constants';
import routes from './routes';
import styles from './style.css';
import {Card, Row, Col, Table, Button, Spin} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
const {Description} = DescriptionList;
export class Detail extends React.PureComponent {
    state = {
        loading: false,
    };

    componentDidMount() {
        this.props.init();
        this.ismounted = true;
    }

    componentWillUnmount() {
        this.ismounted = false;
    }

    render() {
        const {options = []} = this.props.detail;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.setLaborHour) && hasPermissions(options, PERMISSION.setLaborHour))
            operations.push(
                <Button key="setLaborHour" href={`#${routes.laborHour.format(this.props.id)}`} size="small">
                    {this.props.intl.formatMessage({
                        id: 'detail.btn.setLaborHour',
                        defaultMessage: '材料工位维护'
                    })}
                </Button>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.setAssist) && hasPermissions(options, PERMISSION.setAssist))
            operations.push(
                <Button key="setAssist" href={`#${routes.setAssist.format(this.props.id)}`} size="small">
                    {this.props.intl.formatMessage({
                        id: 'detail.btn.setAssist',
                        defaultMessage: '辅助项目绑定'
                    })}
                </Button>
            );
        const {workItemPartDetails = [], assistItems = []} = this.props.detail;
        const columns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'commonDetail.column.partCode',
                    defaultMessage: '备件编号'
                }),
                dataIndex: 'partCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'commonDetail.column.partName',
                    defaultMessage: '备件名称'
                }),
                dataIndex: 'partName',
            }
        ];
        const assistColumns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'commonDetail.assistColumns.workItemCode',
                    defaultMessage: '维修辅助项目编号'
                }),
                dataIndex: 'workItemCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'commonDetail.assistColumns.workItemName',
                    defaultMessage: '维修辅助项目名称'
                }),
                dataIndex: 'workItemName',
            }
        ];
        return (
            <div className={styles.detail}>
                <Spin spinning={this.props.loading || this.state.loading}>

                    <Card>
                        <DescriptionList size="large" title={<Row><Col span={6}>{this.props.intl.formatMessage({
                            id: 'commonDetail.order.title',
                            defaultMessage: '维修项目信息'
                        })}</Col>
                        <Col className={styles.optionBtn} span={18}>{operations}</Col></Row>}>
                            <Description
                                term={this.props.intl.formatMessage({
                                    id: 'commonDetail.info.code',
                                    defaultMessage: '工位编号'
                                })}>
                                {this.props.detail.code}
                            </Description>
                            <Description
                                term={this.props.intl.formatMessage({
                                    id: 'commonDetail.info.name',
                                    defaultMessage: '工位名称'
                                })}>
                                {this.props.detail.name}
                            </Description>
                            <Description
                                term={this.props.intl.formatMessage({
                                    id: 'commonDetail.info.productCategoryCode',
                                    defaultMessage: '车型编号'
                                })}>
                                {this.props.detail.productCategoryCode}
                            </Description>
                            <Description
                                term={this.props.intl.formatMessage({
                                    id: 'commonDetail.info.productCategoryName',
                                    defaultMessage: '车型名称'
                                })}>
                                {this.props.detail.productCategoryName}
                            </Description>
                            <Description
                                term={this.props.intl.formatMessage({
                                    id: 'commonDetail.info.laborHour',
                                    defaultMessage: '工时'
                                })}>
                                {this.props.detail.laborHour}
                            </Description>
                        </DescriptionList>
                        <DescriptionList col={1} size="large">
                            <Description term={this.props.intl.formatMessage({
                                id: 'commonDetail.info.remark',
                                defaultMessage: '备注'
                            })}>
                                {this.props.detail.remark}
                            </Description>
                        </DescriptionList>
                        <DescriptionList col={1} size="large">
                            <Description term={this.props.intl.formatMessage({
                                id: 'commonDetail.info.englishRemark',
                                defaultMessage: '英文备注'
                            })}>
                                {this.props.detail.englishRemark}
                            </Description>
                        </DescriptionList>
                    </Card>
                    <Card
                        title={this.props.intl.formatMessage({
                            id: 'detail.workItemPartDetails.title',
                            defaultMessage: '材料清单'
                        })}>
                        <Table className="white-space-nowrap" rowKey="partId" columns={columns} pagination={false} dataSource={workItemPartDetails} {...TABLE} />
                    </Card>
                    <Card
                        title={this.props.intl.formatMessage({
                            id: 'detail.assistItems.title',
                            defaultMessage: '辅助项目清单'
                        })}>
                        <Table className="white-space-nowrap" rowKey="workItemId" columns={assistColumns} pagination={false} dataSource={assistItems} {...TABLE} />
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" disabled={this.props.loading} onClick={this.props.onCancel}>
                        {this.props.intl.formatMessage({
                            id: 'detail.btn.retrun',
                            defaultMessage: '返回'
                        })}
                    </Button>
                </div>
            </div>
        );
    }
}
Detail.propTypes = {
    detail: PropTypes.object,
    id: PropTypes.string,
    init: PropTypes.func,
    intl: PropTypes.object,
    loading: PropTypes.bool,
    permissions: PropTypes.array,
    onCancel: PropTypes.func,
    onUmount: PropTypes.func
};
import {connect} from 'react-redux';
import {
    getDetail,
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);

const mapStateToProps = (state, ownProps) => ({
    id: ownProps.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']) || state.getIn(['page', 'domainData', 'initData', 'isFetching']),
    detail: getDetailData(state),
    permissions: getPermissions(state),
});

const mapDispatchToProps = (dispatch, ownProps) => ({
    init: () => dispatch(getDetail(ownProps.id)),
    onCancel: () => {
        ownProps.history.goBack();
    },
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(Detail));
